﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace HeathCareSystem
{
    public class ThuocDAO
    {
        DataProvider dp = new DataProvider();

        public bool ThemThuocMoi(ThuocDTO th)
        {
            string strSQL = string.Format("INSERT INTO THUOC (TenThuoc, MaDonViThuoc, DaXoaThuoc) " +
                                     "VALUES (N'{0}', {1}, '{2}')", th.tenThuoc, th.maDonVi, th.daXoaThuoc);
            return dp.ExecuteNonQuery(strSQL);
        }

        public ThuocDTO LayThongTinThuoc(string tenThuoc)
        {
            ThuocDTO thuocDTO = new ThuocDTO();
            string strSQL = string.Format("SELECT * FROM Thuoc WHERE TenThuoc = N'{0}' and DaXoaThuoc = 'False'", tenThuoc);
            DataTable dt = dp.ExecuteQuery(strSQL);
            foreach (DataRow dr in dt.Rows)
            {
                thuocDTO.maThuoc = int.Parse(dr["MaThuoc"].ToString());
                thuocDTO.maDonVi = int.Parse(dr["MaDonViThuoc"].ToString());
                thuocDTO.tenThuoc = dr["TenThuoc"].ToString();
            }
            return thuocDTO;
        }
        // Lấy toàn bộ Loại thuốc
        public List<ThuocDTO> GetAll()
        {
            String strSql = "SELECT * FROM THUOC";
            DataTable dtb = dp.ExecuteQuery(strSql);

            List<ThuocDTO> lstThuoc = new List<ThuocDTO>();

            foreach (DataRow dr in dtb.Rows)
            {
                ThuocDTO ThuocDTO = new ThuocDTO();
                //ThuocDTO.maThuoc = Convert int(dr["MaThuoc"].ToString());
                //ThuocDTO.tenThuoc = dr["TenThuoc"].ToString();
                //ThuocDTO.maDonVi = dr["MaDonVi"].ToString();
                lstThuoc.Add(ThuocDTO);
            }

            return lstThuoc;
        }

        // Xóa
        public bool Delete(string strMaThuoc)
        {
            String strSql = "DELETE FROM THUOC WHERE MaThuoc = '" + strMaThuoc + "'";

            return dp.ExecuteNonQuery(strSql);
        }

        // Update
        public bool Update(ThuocDTO th)
        {
            String strSql = "UPDATE THUOC SET TenThuoc = N'" + th.tenThuoc + "', MaDonViThuoc= " + th.maDonVi + ", DaXoaThuoc='" + th.daXoaThuoc + "'  WHERE MaThuoc = '" + th.maThuoc + "'";

            return dp.ExecuteNonQuery(strSql);
        }

        // Check Tên
        public bool Check_Name(string strTenThuoc)
        {
            String strSql = "SELECT * FROM THUOC WHERE TenThuoc = N'" + strTenThuoc + "'";
            DataTable dtb = dp.ExecuteQuery(strSql);

            if (dtb.Rows.Count > 0)
                return false;
            return true;
        }

        public List<ThuocDTO> TimThuocTheoTen(string tenThuoc)
        {
            List<ThuocDTO> lst = new List<ThuocDTO>();
            try
            {
                DataTable dt = new DataTable();
                string strSQL = string.Format("Select * from THUOC where (TenThuoc Like N'%{0}%')", tenThuoc);
                dt = dp.ExecuteQuery(strSQL);
                foreach (DataRow dr in dt.Rows)
                {
                    ThuocDTO kq = new ThuocDTO();
                    kq.maThuoc = Int32.Parse(dr["MaThuoc"].ToString());
                    kq.tenThuoc = dr["TenThuoc"].ToString();
                    lst.Add(kq);
                }
            }
            catch (Exception)
            {
                lst = null;
            }
            return lst;
        }
    }    
}